<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 16/3/4
 * Time: 下午3:49
 * 上门车订单
 */
class OdsOrderController extends BaseController{
    /*
     * 添加产品包
     * */
    public function save_product(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'product_id' => 'required',
                'product_name' => 'required',
                'product_item' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $order_id = Input::get('order_id'); //订单ID
            $product_id = Input::get('product_id');
            $product_name = Input::get('product_name');
            $product_item = Input::get('product_item');

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            try {
                $item = json_decode($product_item);

                //计算产品包金额
                $amount = 0;
                foreach ($item as $_item) {
                    $amount = $amount + ($_item->amount *  $_item->num);
                }
                $product_package = array(
                    array(
                        'product_id' => $product_id,
                        'product_name' => $product_name,
                        'amount' => $amount,
                        'item' => $item
                    )
                );

                OrderProduct::saveBaseProductsForOds($product_package,'base',$car_data->id,$order_id);

                $result = array(
                    'order_id' => $order_id
                );
                $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
            } catch (Exception $e) {
                Log::error("error", array("system" => "ods" ,"event" => "save_product", 'input' =>Input::all() ,"exception" => $e));
                break;
            }
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "save_product", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 删除产品包
     * */
    public function del_product(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'product_id' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $order_id = Input::get('order_id'); //订单ID
            $product_id = Input::get('product_id');

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            try {
                OrderProduct::delBaseProduct($order_id,$product_id,$car_data->id);

                $result = array(
                    'order_id' => $order_id
                );
                $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
            } catch (Exception $e) {
                Log::error("error", array("system" => "ods" ,"event" => "del_product", 'input' =>Input::all() ,"exception" => $e));
                break;
            }
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "del_product", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 保存30项检测信息
     * */
    public function save_30_check(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'check' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $order_id = Input::get('order_id'); //订单ID
            $check = json_decode(Input::get('check'));

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            try {
                Order30Check::where('order_id',$order_id)->delete();
                foreach($check as $item){
                    foreach(Order30Check::$check as $default){
                        if($default['item_id'] == $item->id){
                            $order_30_check = Order30Check::firstOrNew(array('order_id' => $order_id,'object_id' => $default['object_id'] ,'item_id' =>$item->id));
                            $order_30_check->object_name = $default['object_name'];
                            $order_30_check->item_name = $item->name;
                            $order_30_check->value = $item->value;
                            $order_30_check->status = $item->status;
                            $order_30_check->note = $item->note;
                            $order_30_check->save();
                        }
                    }
                }

                $result = array(
                    'order_id' => $order_id
                );
                $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
            } catch (Exception $e) {
                Log::error("error", array("system" => "ods" ,"event" => "save_30_check", 'input' =>Input::all() ,"exception" => $e));
                break;
            }
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "save_30_check", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 获取30项检测信息
     * */
    public function get_30_check(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $order_id = Input::get('order_id'); //订单ID

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }
            $four_wheel_check = Order30Check::get_four_wheel_check($order_id);
            $cab_check = Order30Check::get_cab_check($order_id);
            $light_check = Order30Check::get_light_check($order_id);
            $engine_room_check = Order30Check::get_engine_room_check($order_id);
            $chassis_check = Order30Check::get_chassis_check($order_id);
            $general_suggest = Order30Check::get_general_suggest($order_id);
            $check = array();
            array_push($check,$four_wheel_check);
            array_push($check,$cab_check);
            array_push($check,$light_check);
            array_push($check,$engine_room_check);
            array_push($check,$chassis_check);
            array_push($check,$general_suggest);
            $result = array(
                'order_id' => $order_id,
                'check' => $check
            );
//            $result = array(
//                'four_wheel_check' => $four_wheel_check,
//                'cab_check' => $cab_check,
//                'light_check' => $light_check,
//                'engine_room_check' => $engine_room_check,
//                'chassis_check' => $chassis_check,
//                'general_suggest' => $general_suggest,
//            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "sce" ,"event" => "get_30_check", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 订单出发
     * */
    public function departure(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $order_id = Input::get('order_id');

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            try{
                $order = Orders::where('order_id',$order_id)->select(array('status'))->first();
                if($order->status != Config::get('order.service_confirm_order_status')){
                    $response = Lang::get("ods_error.order_status_error_12009");
                    break;
                }else{
                    $car_part_count = MaintenanceCarParts::where('order_id',$order_id)->where('status',1)->count();
                    if($car_part_count == 0){
                        $order_status = Config::get('order.engineer_departure_order_status');
                    }else{
                        $response = Lang::get("ods_error.order_part_error_12012");
                        break;
                    }
                }
                //订单信息 update
                Orders::where('order_id',$order_id)->update(
                    array(
                        'status' => $order_status
                    )
                );

                //订单流转表新增记录
                $order_flows = new OrderFlows;
                $order_flows->order_id = $order_id;
                $order_flows->status = $order_status;
                $order_flows->user_id = $car_data->id;
                $order_flows->type = 'car';
                $order_flows->created_at = Common::num2DateTime(time());
                $order_flows->save();

                $result = array(
                    'order_id' => $order_id,
                    'status' => $order_status
                );

                /*发送短信*/
                SendMessage::sendSms('engineer_departure_order_status',$order_id);
            } catch (Exception $e) {
                Log::error("error", array("system" => "ods" ,"event" => "departure", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ods_error.order_update_error_12010");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "departure", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    /*
     * 订单到达
     * */
    public function arrival(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $order_id = Input::get('order_id');

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            try{
                $order = Orders::where('order_id',$order_id)->select(array('status'))->first();
                if($order->status != Config::get('order.engineer_departure_order_status')){
                    $response = Lang::get("ods_error.order_status_error_12009");
                    break;
                }else{
                    $order_status = Config::get('order.engineer_arrive_order_status');
                }
                //订单信息 update
                Orders::where('order_id',$order_id)->update(
                    array(
                        'status' => $order_status
                    )
                );

                //订单流转表新增记录
                $order_flows = new OrderFlows;
                $order_flows->order_id = $order_id;
                $order_flows->status = $order_status;
                $order_flows->user_id = $car_data->id;
                $order_flows->type = 'car';
                $order_flows->created_at = Common::num2DateTime(time());
                $order_flows->save();

                $result = array(
                    'order_id' => $order_id,
                    'status' => $order_status
                );

                /*发送短信*/
                SendMessage::sendSms('engineer_arrive_order_status',$order_id);
            } catch (Exception $e) {
                Log::error("error", array("system" => "ods" ,"event" => "arrival", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ods_error.order_update_error_12010");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "arrival", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 订单服务开始
     * */
    /**
     * @Author:      lyt
     * @DateTime:    2016-04-28 15:14:02
     * @Description: 添加订单开始的时间  添加车辆认证的逻辑
     */
    public function service_begin(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required'
            )
        );
        $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $order_id = Input::get('order_id');

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            try{
                $order = Orders::where('order_id',$order_id)->select(array('status',"user_car_id"))->first();
                if($order->status != Config::get('order.engineer_arrive_order_status')){
                    $response = Lang::get("ods_error.order_status_error_12009");
                    break;
                }else{
                    $order_status = Config::get('order.engineer_service_begin_order_status');
                }
                $uci=UserCarInfo::find($order->user_car_id);
                $csi=CarStandardInfo::find($uci->car_model_id);
                if($uci->is_lock!=1){//如果该车以前不是认证车辆 修改该车为认证车辆
                    //修改其他车型不同的的认证车辆 认证状态为未认证
                    UserCarInfo::where("car_no",$uci->car_no)->where("is_lock",1)->where("car_model_id","!=",$csi->id)->update(array("is_lock"=>0));
                    UserCarInfo::where("id",$uci->id)->update(array("is_lock"=>1));
//                    $uci->is_lock=1;
//                    $uci->save();
                    $uacs=UserAuthenticationCar::where('car_no',$uci->car_no)->where('car_model_id',$csi->id)->where('status',1)->get();
                    if(count($uacs)==0){
                        UserAuthenticationCar::where("car_no",$uci->car_no)->update(array("status"=>0));
                        $uac=new UserAuthenticationCar();
                        $uac->status=1;
                        $uac->car_no=$uci->car_no;
                        $uac->vin_code=$uci->vin_code;
                        $uac->mileage=$uci->mileage;
                        $uac->car_brand_id=$uci->car_brand_id;
                        $uac->car_series_id=$uci->car_series_id;
                        $uac->car_model_id=$uci->car_model_id;
                        $uac->user_id=$uci->user_id;
                        $uac->shop_id=$sc_user->shop_id;
                        $uac->create_by=0;
                        $uac->save();
                    }
                }

                //订单信息 update
                Orders::where('order_id',$order_id)->update(
                    array(
                        'status' => $order_status,
                        'start_time'=>Common::num2DateTime(time(),"Y-m-d H:i:s"),
                    )
                );

                //订单流转表新增记录
                $order_flows = new OrderFlows;
                $order_flows->order_id = $order_id;
                $order_flows->status = $order_status;
                $order_flows->user_id = $car_data->id;
                $order_flows->type = 'car';
                $order_flows->created_at = Common::num2DateTime(time());
                $order_flows->save();

                $result = array(
                    'order_id' => $order_id,
                    'status' => $order_status
                );
            } catch (Exception $e) {
                Log::error("error", array("system" => "ods" ,"event" => "service_begin", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ods_error.order_update_error_12010");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "service_begin", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 订单服务完成（复检确认）
     * */
    public function service_end(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $order_id = Input::get('order_id');

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            try{
                $order = Orders::where('order_id',$order_id)->select(array("mileage",'status'))->first();
                if($order->status != Config::get('order.engineer_service_begin_order_status')){
                    $response = Lang::get("ods_error.order_status_error_12009");
                    break;
                }else{
                    $order_status = Config::get('order.engineer_service_end_order_status');
                }
                if(!$order->mileage){
                    $response = Lang::get("ods_error.order_mileage_error_12015");
                    break;
                }
                //订单信息 update
                Orders::where('order_id',$order_id)->update(
                    array(
                        'status' => $order_status
                    )
                );

                //订单流转表新增记录
                $order_flows = new OrderFlows;
                $order_flows->order_id = $order_id;
                $order_flows->status = $order_status;
                $order_flows->user_id = $car_data->id;
                $order_flows->type = 'car';
                $order_flows->created_at = Common::num2DateTime(time());
                $order_flows->save();

                //修改配件使用状态
                $order_product_id = OrderProduct::where('order_id',$order_id)->where('status',1)->select(array('id'))->get()->toArray();
                if(!is_null($order_product_id)){
                    MaintenanceCarParts::where('order_id',$order_id)
                        ->whereIn('order_product_package_id',$order_product_id)
                        ->where('status',2)
                        ->update(
                            array(
                                'status' => 4
                            )
                        );
                }

                $result = array(
                    'order_id' => $order_id,
                    'status' => $order_status
                );
            } catch (Exception $e) {
                Log::error("error", array("system" => "ods" ,"event" => "service_end", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ods_error.order_update_error_12010");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "service_end", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 订单支付
     * */
    public function pay(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'amount' => 'required',
                'pay_type' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $order_id = Input::get('order_id');
            $amount = Input::get('amount');
            $pay_type = Input::get('pay_type');
            $pay_time = Common::num2DateTime(time()+Config::get("config.report_time_diff"));

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            try{
                $order = Orders::where('order_id',$order_id)->select(array('status',"mileage","user_car_id"))->first();
                if($order->status != Config::get('order.engineer_service_end_order_status')){
                    $response = Lang::get("ods_error.order_status_error_12009");
                    break;
                }else{
                    $order_status = Config::get('order.engineer_service_recheck_status');
                }
                //订单反算金额
                Orders::inverseOrder($order->order_id);

                $pay_array=array(array("pay_type"=>$pay_type,"amount"=>$amount));
                $response=Orders::pay($order_id,$pay_array,$car_data->id,"car",$order_status);
                if($response["code"]!=0){
                    break;
                }


                //订单信息 update
                // Orders::where('order_id',$order_id)->update(
                //     array(
                //         'status' => $order_status,
                //         'pay_time' => $pay_time
                //     )
                // );
                //更改用户车辆里程
                $uci=UserCarInfo::find($order->user_car_id);
                $uci->mileage =$order->mileage;//
                $uci->save();

                //订单流转表新增记录
                $order_flows = new OrderFlows;
                $order_flows->order_id = $order_id;
                $order_flows->status = $order_status;
                $order_flows->user_id = $car_data->id;
                $order_flows->type = 'car';
                $order_flows->created_at = Common::num2DateTime(time());
                $order_flows->save();

                $result = array(
                    'order_id' => $order_id,
                    'status' => $order_status
                );

                /*发送短信*/
                SendMessage::sendSms('custom_pay_order_status',$order_id);

                /*邀请完成订单送券*/
                Coupons::invite($order_id);
            } catch (Exception $e) {
                Log::error("error", array("system" => "ods" ,"event" => "pay", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ods_error.order_update_error_12010");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "pay", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 订单服务终止
     * */
    public function abort(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required',
                'remark' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $order_id = Input::get('order_id');
            $remark = Input::get('remark');
            $amount = Input::get('amount');
            $pay_type = Input::get('pay_type')?Input::get('pay_type'):0;

            //验证token
            $car_data = MaintenanceCars::getMaintenanceCarByToken($token);
            if(is_numeric($car_data) && $car_data < 0){
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            try{
                $order = Orders::where('order_id',$order_id)->select(array('status',"mileage","user_car_id"))->first();
                $order_allow_abort_status = array('1213','1214','1215');
                if(!in_array($order->status,$order_allow_abort_status)){
                    $response = Lang::get("ods_error.order_status_error_12009");
                    break;
                }else{
                    //技师终止订单
                    $order_status = Config::get('order.engineer_abort_order_status');
                }
                //订单信息 update
                Orders::where('order_id',$order_id)->update(
                    array(
                        'status' => $order_status,
                    )
                );
                //更改用户车辆里程
                $uci=UserCarInfo::find($order->user_car_id);
                $uci->mileage =$order->mileage;//
                $uci->save();


                //订单流转表新增记录
                $order_flows = new OrderFlows;
                $order_flows->order_id = $order_id;
                $order_flows->status = $order_status;
                $order_flows->user_id = $car_data->id;
                $order_flows->type = 'car';
                $order_flows->created_at = Common::num2DateTime(time());
                $order_flows->save();

                //记录终止原因
                $order_remark = new OrderRemark;
                $order_remark->order_id = $order_id;
                $order_remark->status =$order_status;
                $order_remark->remark = $remark;
                $order_remark->user_id = $car_data->id;
                $order_remark->user_type = 'car';
                $order_remark->save();

                if(!is_null($amount)){
                    $pay_time = Common::num2DateTime(time());
                    Orders::where('order_id',$order_id)->update(
                        array(
                            'payable_amount' => $amount,
                            'pay_type' => $pay_type,
                            'pay_time' => $pay_time
                        )
                    );
                }

                $result = array(
                    'order_id' => $order_id,
                    'status' => $order_status
                );
            } catch (Exception $e) {
                Log::error("error", array("system" => "ods" ,"event" => "abort", 'input' =>Input::all() ,"exception" => $e));
                $response = Lang::get("ods_error.order_update_error_12010");
                break;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "abort", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 获取订单列表
     * */
    public function get_order_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'shop_id' => 'required',
                'car_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $shop_id = Input::get('shop_id');
            $car_id = Input::get('car_id');
            $date = Input::get('date')?Input::get('date'):date('Y-m-d',time());

            //验证token
            $car_info = CarTokens::where('token',$token)->select(array('car_id'))->first();
            if(!is_null($car_info)){
                //增加订单时间限制
                $day_diff = abs(time() - Common::datetime2Num($date))/(60*60*24);
                if($day_diff > 3){
                    $response = Lang::get("ods_error.order_time_error_12013");
                    break;
                }

                $orders = Orders::where('car_id',$car_id)
//                    ->where('shop_id',$shop_id)
                    ->leftJoin('car_brand_type','car_brand_type.id','=','orders.car_brand_id')
                    ->where('book_time',$date)
//                    ->where('status',Config::get('order.service_confirm_order_status'))
                    ->select(array('order_id','book_time','book_hour','orders.status','car_brand_type.icon'))
                    ->orderBy('book_hour','desc')
                    ->get()
                    ->toArray();
                foreach($orders as $key=>$order){
                    $orders[$key]['icon']=Config::get('config.car_brand_img_url').$order['icon'];
                }
            }else{
                Log::error("error", array("system" => "ods" ,"event" => "get_order_list", 'input' =>Input::all()));
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => array(
                    'count' => count($orders),
                    'date' => $date,
                    'orders' => $orders
                )
            );
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "get_order_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 打印接口
     * */
    public function prints(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required'
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $order_id = Input::get('order_id');

            //验证token
            $car_info = CarTokens::where('token',$token)->select(array('car_id'))->first();
            if(!is_null($car_info)){
                $order=Orders::where('order_id',$order_id)->select('order_id','balance_amount','discount_amount','start_time','end_time','work_service','payable_amount','amount')->first();
                if(is_null($order)){
                    $response = Lang::get("ods_error.order_not_exist_error_12006");
                    break;
                }
                if(is_null($order->amount)) $order->amount = 0;
                if(is_null($order->discount_amount)) $order->discount_amount = 0;
                if(is_null($order->payable_amount)) $order->payable_amount = 0;
                if(is_null($order->balance_amount)) $order->balance_amount = 0;
                $o3c=Order30Check::where('order_id',Input::get('order_id'))->get();
                $orc=OrderRingCheck::find(Input::get('order_id'));
                foreach($o3c as $_o3c){
                    if(!in_array($_o3c->item_id,array('left_front_tire_pressure','left_front_tread','left_front_brake_block'))){
                        $_o3c->value=' ';
                    }
                }
                $temp_arr=array('order_30_check'=>$o3c,'order_ring_check'=>$orc);
                $orderProduct=OrderProduct::getOrderProductById($order->order_id);
                $temp1=$temp2=$temp3=$item=array();
                $service=0;
                foreach($orderProduct['promotion_procut'] as $pp){
                    foreach($opp->package as $p){
                            foreach($p as $_p){
                                if($_p->type==1){
                                    $temp1['item_id']=$_p->item_id;
                                    $temp1['item_name']=$_p->item_name;
                                    $temp1['num']=$_p->num;
                                    $temp1['amount']=$_p->amount;
                                    $item[]=$temp1;
                                }else{
                                    $service+=$_p->amount;
                                }
                            }
                    }
                }
                foreach($orderProduct['base_procut'] as $bp){
                    foreach($bp->item as $_bp){
                        if($_bp->type==1){
                            $temp2['item_id']=$_bp->item_id;
                            $temp2['item_name']=$_bp->item_name;
                            $temp2['num']=$_bp->num;
                            $temp2['amount']=$_bp->amount;
                            $item[]=$temp2;
                        }else{
                            $service+=$_bp->amount;
                        }
                    }
                }
                foreach($orderProduct['non_standard'] as $ns){
                    foreach($ns->item as $_ni){
                        if($_ni->service_type==1){
                            $temp3['item_id']=$_ni->item_id;
                            $temp3['item_name']=$_ni->item_name;
                            $temp3['num']=$_ni->num;
                            $temp3['amount']=$_ni->amount;
                            $item[]=$temp3;
                        }else{
                            $service+=$_ni->amount;
                        }
                    }
                }
                $service+=$order->work_service;
                $order->service=$service;
                $result=array("order_info"=>$order,"product_info"=>$item,"check_info"=>$temp_arr);
            }else{
                Log::error("error", array("system" => "ods" ,"event" => "show", 'input' =>Input::all()));
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $result
            );
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "show", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 获取订单详情
     * */
    public function show(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'order_id' => 'required'
            )
            );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("ods_error.args_miss_error_12001");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $order_id = Input::get('order_id');

            //验证token
            $car_info = CarTokens::where('token',$token)->select(array('car_id'))->first();
            if(!is_null($car_info)){
                $order=Orders::find($order_id);
                if(is_null($order)){
                    $response = Lang::get("ods_error.order_not_exist_error_12006");
                    break;
                }
                if(is_null($order->amount)) $order->amount = 0;
                if(is_null($order->discount_amount)) $order->discount_amount = 0;
                if(is_null($order->payable_amount)) $order->payable_amount = 0;
                if(is_null($order->balance_amount)) $order->balance_amount = 0;

                if($order->user_car_id != 0){
                    $uci=UserCarInfo::find($order->user_car_id);
                    if(is_null($uci)){
                        $response = Lang::get("ods_error.order_car_info_not_exist_error_12007");
                        break;
                    }

                    $csi=CarStandardInfo::find($uci->car_model_id);
                    if(is_null($csi)){
                        $response = Lang::get("scs_error.order_car_model_not_exist_error_12008");
                        break;
                    }

                    $car_arr=array(
                        "car_no"=>$uci->car_no,
                        "kilometre"=>$order->mileage,
                        "befor_kilometre"=>$uci->mileage,
                        "vin"=>$uci->vin_code,
                        "note"=>$uci->note,
                        "desc"=>"",
                        "brand_name"=>$csi->brand_name,
                        "series_name"=>$csi->series_name,
                        "model_name"=>$csi->model_name,
                        "model_id"=>$csi->id,
                        "user_car_id"=>$order->user_car_id,
                    );
                }else{
                    $car_arr = array(
                        "car_no"=>'未知',
                        "kilometre"=>'未知',
                        "befor_kilometre"=>'未知',
                        "vin"=>'未知',
                        "note"=>'未知',
                        "desc"=>"",
                        "brand_name"=>'未知',
                        "series_name"=>'未知',
                        "model_name"=>'未知',
                        "model_id"=>0,
                        "user_car_id"=>0,
                    );
                }

                $orderProduct=OrderProduct::getOrderProductById($order->order_id);
                $temp_arr=array("order_info"=>$order,"user_car_info"=>$car_arr,"product_info"=>$orderProduct);
            }else{
                Log::error("error", array("system" => "ods" ,"event" => "show", 'input' =>Input::all()));
                $response = Lang::get("ods_error.token_check_error_12005");
                break;
            }

            //组织返回
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => $temp_arr
            );
        } while (false);
        Log::info("info", array("system" => "ods" ,"event" => "show", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}